<template>
  <div class="login_container" style="background-color: violet;">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../../assets/154216.jpg" alt="" />
      </div>
      <!-- 登录表单 -->
      <el-form :model="user" :rules="rules" label-width="0px" class="login_form" ref="LoginFormRef">
        <el-form-item prop="zhau" placeholder="请输入账号">
          <el-input v-model="user.account" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password" placeholder="请输入密码">
          <el-input v-model="user.password" prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="user.code" auto-complete="off" placeholder="请输入验证码" size=""></el-input>
          <div class="login-code" width="100%" @click="refreshCode">
            <!--验证码组件-->
            <Authcode :identifyCode="identifyCode"></Authcode>
          </div>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" round style="background-color: lightcoral; border: 1px solid #eee"
            @click="login()">登录</el-button>
          <!-- <el-button type="info" round style="background-color: #ddd; border: 1px solid #eee"
            @click="resetForm">重置</el-button> -->
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Authcode from '../all/authcode.vue'
export default {
  components: {
    Authcode,
  },
  name: 'HelloWorld',
  data() {
    return {
      user: {
        account: "comm",
        password: "123456",
        code: ""
      },
      //验证码
      number: this.$route.params.loginnumber,
     // identifyCodes: '1234567890abcdefjhijklinopqrsduvwxyz',//随机串内容
      identifyCodes: '1',//随机串内容
      identifyCode: '',
      // 校验
      rules: {
        account: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
      }
    }
  },
  created: function () {
    // 初始化验证码
    this.identifyCode = ''
    this.makeCode(this.identifyCodes, 4)
  },
  mounted: function () {
    var options = {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0,
    };

    function success(pos) {
      var crd = pos.coords;

      console.log("Your current position is:");
      // longtitude: String(result.longitude).match(/\d+\.\d{0,6}/)[0],
      //   latitude: String(result.latitude).match(/\d+\.\d{0,6}/)[0],

      console.log("Latitude : " + String(crd.latitude).match(/\d+\.\d{0,6}/)[0]);
      console.log("Longitude: " + String(crd.longitude).match(/\d+\.\d{0,6}/)[0]);
      console.log("More or less " + crd.accuracy + " meters.");
      let latitude = crd.latitude.toFixed(6)
      let longitude = crd.longitude.toFixed(6)
      window.localStorage.setItem("latitude", latitude)
      window.localStorage.setItem("longitude", longitude)
    }

    function error(err) {
      console.warn("ERROR(" + err.code + "): " + err.message);
    }

    navigator.geolocation.getCurrentPosition(success, error, options);
  },
  methods: {
    //验证码校验
    refreshCode() {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4)
    },
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[this.randomNum(0, this.identifyCodes.length)]
      }
    },
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min)
    },
    login: function () {
      //登录请求前验证码校验
      if (this.user.code.toLowerCase() !== this.identifyCode.toLowerCase()) {
        this.$message.error('请填写正确验证码')
        this.refreshCode()
        return
      }
      //Buffer.from   btoa对数据进行base64编码
      let basic = "Basic " + btoa("woniuxy:123", "utf8");
      let url = `oauth/token?grant_type=password&username=` + this.user.account + `&password=` + this.user.password;

      this.$axios.post(url, {}, {
        headers: {
          Authorization: basic
        }
      }).then(res => {
        console.log(res)
        if (res.status == 200) {
          this.$router.push("/home")
          window.localStorage.setItem("access_token", res.data.access_token);
          window.localStorage.setItem("refresh_token", res.data.refresh_token);
          window.localStorage.setItem("scope", res.data.scope);
          window.localStorage.setItem("uid", res.data.uid);
          window.localStorage.setItem("account", res.data.account);
          console.log(localStorage);
        } else {
          this.$message.error("请检查账号和密码是否正确！");
        }

      })

      this.$axios.get("user/password/" + this.user.account + "/" + this.user.password).then(res => {
        console.log(res);
        if (res.data.code== 501) {
          this.$message.error("密码输入错误")

        } else if (res.data.code== 500) {
          this.$message.error("账号不存在")
        }
      });
    },


  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: rgb(218, 72, 72);
  height: 100%;
}

.login_box {
  width: 450px;
  height: 400px;
  background: #fff;
  border-radius: 3px;
  box-shadow: 0 0 10px #ddd; //阴影
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px; //头像图片和头像盒子的距离
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
  }

  .avatar_box img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }

}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
  background-color: #eee;
}
</style>